package cn.moningxi.controller;

import cn.moningxi.bean.GoodsCategory;
import cn.moningxi.service.GoodsCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
public class GoodsCategoryController {

    @Autowired
    GoodsCategoryService goodsCategoryService;

    /**
     * 查询全部一级分类
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping("/categoryPage")
    public ModelAndView getAllCategory(ModelAndView modelAndView) {
        List<GoodsCategory> categoryList = goodsCategoryService.getAllCategory();
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("backstage/product-category");
        return modelAndView;
    }

    /**
     * 查询当前类别的子类
     *
     * @param modelAndView
     * @param categoryId
     * @return
     */
    @RequestMapping("/childCate/{categoryId}")
    public ModelAndView getChildCategory(ModelAndView modelAndView, @PathVariable("categoryId") Integer categoryId) {
        List<GoodsCategory> categoryList = goodsCategoryService.getChildCategory(categoryId);
        modelAndView.addObject("categoryList", categoryList);
        modelAndView.setViewName("backstage/product-category");
        return modelAndView;
    }

    /**
     * 跳转到修改分类信息界面
     *
     * @param modelAndView
     * @param categoryId
     * @return
     */
    @RequestMapping("/editCate/{categoryId}")
    public ModelAndView editCate(ModelAndView modelAndView, @PathVariable("categoryId") Integer categoryId) {
        GoodsCategory goodsCategory = goodsCategoryService.selectCateById(categoryId);
        modelAndView.addObject("goodsCategory", goodsCategory);
        modelAndView.setViewName("backstage/product-category-edit");
        return modelAndView;
    }

    /**
     * 将修改后的分类信息资料更新到数据中
     *
     * @param modelAndView
     * @param submitCategoryInfo
     * @return
     */
    @RequestMapping(value = "/updateCateInfo", method = RequestMethod.POST)
    public ModelAndView updateCateInfo(ModelAndView modelAndView, GoodsCategory submitCategoryInfo) {
        goodsCategoryService.updateCateInfo(submitCategoryInfo);
        String id = submitCategoryInfo.getParentId();
        modelAndView.setViewName("redirect:/childCate/" + id);
        return modelAndView;
    }

    /**
     * 跳转到添加商品类别页面
     *
     * @param modelAndView
     * @param pid
     * @return
     */
    @RequestMapping("/addCategoryPage/{pid}")
    public ModelAndView addCategoryPage(ModelAndView modelAndView, @PathVariable("pid") String pid) {
        modelAndView.addObject("pid", pid);
        modelAndView.setViewName("backstage/product-category-add");
        return modelAndView;
    }

    /**
     * 添加商品分类
     *
     * @param modelAndView
     * @param submitAddCategoryInfo
     * @return
     */
    @RequestMapping(value = "/addCategory", method = RequestMethod.POST)
    public ModelAndView addCategory(ModelAndView modelAndView, GoodsCategory submitAddCategoryInfo) {
        goodsCategoryService.addCategory(submitAddCategoryInfo);
        String id = submitAddCategoryInfo.getParentId();
        modelAndView.setViewName("redirect:/childCate/" + id);
        return modelAndView;
    }

}
